package com.baidu.dao.util;

import com.baidu.common.bean.Role;
import com.baidu.common.bean.User;
import com.baidu.common.exception.WebException;
import com.baidu.common.finalword.JdbcConstant;
import com.baidu.common.util.PropertiesUtil;
import com.mysql.jdbc.StringUtils;

import java.math.BigDecimal;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: BSS
 * @create: 2020/8/19/17:31
 */
public class JdbcUtil {
    private static Connection connection;
    private static Statement statement;
    public static final String PROFILE_PATH = "/JdbcInfo.properties";
    public static final String PROFILE_SQL = "/JdbcSql.properties";
    private static String jdbcPath;
    private static String jdbcUserName;
    private static String jdbcPassword;

    static {
        try {
            PropertiesUtil.reload(PROFILE_PATH);
            Class.forName(PropertiesUtil.getString(JdbcConstant.JDBC_DRIVER));
            jdbcPath = PropertiesUtil.getString(JdbcConstant.JDBC_PATH);
            jdbcUserName = PropertiesUtil.getString(JdbcConstant.JDBC_USERNAME);
            jdbcPassword = PropertiesUtil.getString(JdbcConstant.JDBC_KEY);
            connection = DriverManager.getConnection(jdbcPath,jdbcUserName,jdbcPassword);
            connection.setAutoCommit(false);
        } catch (WebException | ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }

    private JdbcUtil(){}


    public static Connection getConnection() throws WebException {
        if (connection==null){
            synchronized (JdbcUtil.class){
                if (connection==null){
                    try {
                        connection = DriverManager.getConnection(jdbcPath, jdbcUserName, jdbcPassword);
                        return connection;
                    } catch (SQLException e) {
                        e.printStackTrace();
                        throw new WebException(e.getMessage());
                    }
                }
            }
        }
        return connection;
    }

    public static ResultSet executeQuery(String sql) throws WebException {
        try {
            statement = connection.createStatement();
            return statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException("数据库查询异常");
        }
    }

    public static int executeUpdate(String sql) throws WebException {
        try {
            statement = connection.createStatement();
            return statement.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException("数据库修改或删除异常");
        }
    }

    public static void closeStatement(Statement statement) throws WebException {
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new WebException(e.getMessage());
            }
        }
    }

    public static void closeResultSet(ResultSet resultSet) throws WebException {
        if (resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
                throw new WebException(e.getMessage());
            }
        }
    }

    public static User getUser(ResultSet resultSet) throws WebException {
        try {
            User user = new User();
            while (resultSet.next()){
                user = getUserInfo(resultSet);
            }
            return user;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException("用户查询时异常");
        }
    }

    public static List<User> getUsers(ResultSet resultSet) throws WebException {
        try {
            List<User> userList = new ArrayList<>();
            while (resultSet.next()){
                User user = getUserInfo(resultSet);
                userList.add(user);
            }
            return userList;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new WebException("用户查询时异常");
        }
    }

    public static User getUserInfo(ResultSet resultSet) throws SQLException, WebException {
        Integer id = resultSet.getInt("id");
        String name = resultSet.getString("userName");
        String account = resultSet.getString("account");
        String password = resultSet.getString("password");
        BigDecimal balance = resultSet.getBigDecimal("balance");
        String roleId = resultSet.getString("roleId");

        User user = new User();

        user.setId(id);
        user.setUserName(name);
        user.setAccount(account);
        user.setPassword(password);
        user.setBalance(balance);
        user.setRoles(getRoles(roleId));

        if (StringUtils.isNullOrEmpty(user.getAccount())){
            return null;
        }

        return user;
    }

    private static List<Role> getRoles(String roleIdsStr) throws WebException {
        List<Role> roleList = new ArrayList<>();

        if (StringUtils.isNullOrEmpty(roleIdsStr)){
            return roleList;
        }

        String[] split = roleIdsStr.split("[-]");

        try {
            for (String roleId : split) {
                String roleSql = "select * from tbl_role where id="+Integer.parseInt(roleId);
                ResultSet roleResult = JdbcUtil.executeQuery(roleSql);
                while (roleResult.next()){
                    Role role = new Role();
                    role.setId(roleResult.getInt("id"));
                    role.setName(roleResult.getString("name"));
                    roleList.add(role);
                }
                roleResult.close();
            }
            return roleList;
        } catch (WebException | SQLException e) {
            e.printStackTrace();
            throw new WebException("查询用户角色时异常");
        }
    }
}
